//
//  ZHAnchorDetailViewController.m
//  XMLYFM
//
//  Created by dllo on 15/12/24.
//  Copyright © 2015年 zzl. All rights reserved.
//

#import "ZHAnchorDetailViewController.h"
#import "AppTools.h"
#import "BaseLabel.h"
#import "UIImageView+WebCache.h"
#import "Masonry.h"
#import "ZHAnchorDetailModel.h"
#import "ZHAnchorDetailTableViewCell.h"
#import "SizeForLabel.h"
#import "FXBlurView.h"
#import "MJRefresh.h"
#import "ZHPlayViewController.h"
#import "ZHPlayButtonView.h"

@interface ZHAnchorDetailViewController ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate>

@property (nonatomic,strong) UIImageView *bgImageView;// 背景图片
@property (nonatomic,strong) UIImageView *anchorImage;// 主播图片
@property(nonatomic,retain) UILabel *trackTitle;// 主播名
@property (nonatomic,strong) BaseLabel *personDescribe;// 主播简介
@property(nonatomic,retain)UIImageView *overImage;

@property(nonatomic,retain)UIView *tableHeadView;
@property (nonatomic,strong) UITableView *dTableView;
@property (nonatomic,strong) NSMutableArray *detailArray;

@property (nonatomic,strong) FXBlurView *blurView;

@property(nonatomic,assign) NSInteger currentPage;

@end

@implementation ZHAnchorDetailViewController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[ZHPlayButtonView shareButton].musicBtn addTarget:self action:@selector(buttonActionz:) forControlEvents:UIControlEventTouchUpInside];
    
    if ([ZHPlayViewController sharaMusicPlayer].player.state == STKAudioPlayerStatePlaying) {
        
        [[ZHPlayButtonView shareButton] startCircle];
    }
    if ([ZHPlayViewController sharaMusicPlayer].player.state == STKAudioPlayerStatePaused) {
        
        [[ZHPlayButtonView shareButton] stopCircle];
        
    }
    
    [self.view bringSubviewToFront:self.messageView];
    self.messageView.center = CGPointMake(SCREEN_WIDTH_ZH/2,  SCREEN_HEIGHT_ZH *0.85);
}
// buttonView单例
- (void)buttonActionz:(UIButton *)btn{
    
    ZHPlayViewController *player = [ZHPlayViewController sharaMusicPlayer];
    ZHNavigationBarController *naVC = [[ZHNavigationBarController alloc] initWithRootViewController:player];
    
    [self presentViewController:naVC animated:YES completion:^{
        
    }];

}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    _detailArray = [NSMutableArray array];
    [self getAuthorData];
    [self getData];
    [self createSubviews];
    
    [self setAutomaticallyAdjustsScrollViewInsets:NO];

    // 返回按钮
    UIButton * backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_tableHeadView addSubview:backButton];
    backButton.frame = CGRectMake(20, 0 , 20, 30);
    backButton.backgroundColor = [UIColor clearColor];
    [backButton setImage:[UIImage imageNamed:@"btn_back_n@2x.png"] forState:UIControlStateNormal];
    [backButton setImage:[UIImage imageNamed:@"btn_back_h@2x.png"] forState:UIControlStateHighlighted];
    [backButton addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
//    [_tableHeadView bringSubviewToFront:backButton];
    backButton.tag = 5555;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];

}

#pragma mark - 返回按钮方法
-(void)backButtonAction:(UIButton *)btn
{
    [self.navigationController dismissViewControllerAnimated:YES completion:^{
        
        
    }];
    
}
#pragma mark - 创建子视图
-(void)createSubviews
{
    
    // 头部背景图
    _bgImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH_ZH, SCREEN_HEIGHT_ZH*0.28)];
    self.bgImageView.alpha = 0.5;
    [self.view addSubview:_bgImageView];
    //    [_bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
    //
    //        make.top.left.mas_equalTo(@0);
    //        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH_ZH, SCREEN_HEIGHT_ZH*0.28));
    //    }];
    
    _blurView = [[FXBlurView alloc]initWithFrame:_bgImageView.bounds];
    [_bgImageView addSubview:_blurView];
    _blurView.blurRadius = 100;
    

    
    _dTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH_ZH, SCREEN_HEIGHT_ZH) style:UITableViewStylePlain];
    _dTableView.delegate = self;
    _dTableView.dataSource = self;
    _dTableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_dTableView];
    // 注册
    [_dTableView registerClass:[ZHAnchorDetailTableViewCell class] forCellReuseIdentifier:@"ZHAnchorDetailTableViewCell"];
    
    
    self.currentPage = 1;
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(getData)];
    self.dTableView.mj_footer = footer;
    
    self.tableHeadView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH_ZH, SCREEN_HEIGHT_ZH*0.28)];
    self.dTableView.tableHeaderView = self.tableHeadView;
    [self creatHeadView];

    [ZHPlayButtonView shareButton].frame = CGRectMake(SCREEN_WIDTH_ZH*0.4, SCREEN_HEIGHT_ZH-SCREEN_WIDTH_ZH*0.2, SCREEN_WIDTH_ZH*0.2, SCREEN_HEIGHT_ZH*0.2);
    [self.view addSubview:[ZHPlayButtonView shareButton]];
    
    
}
#pragma mark - 创建头视图
-(void)creatHeadView
{
    
    self.tableHeadView.backgroundColor = [UIColor clearColor];
    // 主播图片
    _anchorImage = [[UIImageView alloc]init];
    [_tableHeadView addSubview:_anchorImage];
    [_anchorImage mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(_bgImageView);
        make.width.height.mas_equalTo(@60);
        make.top.mas_equalTo(SCREEN_HEIGHT_ZH*0.05);
        
    }];
    self.anchorImage.layer.masksToBounds = YES;
    self.anchorImage.layer.cornerRadius = SCREEN_WIDTH_ZH*0.1;
    self.anchorImage.layer.borderColor = [UIColor whiteColor].CGColor;
    self.anchorImage.layer.borderWidth = 1;

    
    
    //主播名
    self.trackTitle = [[UILabel alloc] init];
    self.trackTitle.backgroundColor = [UIColor clearColor];
    self.trackTitle.textColor = [UIColor whiteColor];
    self.trackTitle.font = [UIFont systemFontOfSize:13 weight:1];
    self.trackTitle.center = CGPointMake(SCREEN_WIDTH_ZH/2, SCREEN_HEIGHT_ZH*0.18);
    [self.tableHeadView addSubview:self.trackTitle];
    [_trackTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(_anchorImage);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH_ZH*0.5, SCREEN_HEIGHT_ZH*0.03));
        make.top.equalTo(_anchorImage.mas_bottom).offset(5);
    }];
    
    // 主播介绍
    _personDescribe = [[BaseLabel alloc]init];
    [_tableHeadView addSubview:_personDescribe];
    _personDescribe.textAlignment = NSTextAlignmentCenter;
    [_personDescribe mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(_anchorImage.mas_bottom).offset(8);
        make.width.mas_equalTo(SCREEN_WIDTH_ZH);
        make.height.mas_equalTo(@40);
    }];
    

    
}
#pragma mark - 请求数据
-(void)getAuthorData
{
     NSString *urlStr = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/homePage?position=2&toUid=%ld&device=iphone&title=%@", _uid, _titleName];

//    NSLog(@"ssss %@",urlStr);
    [AppTools getDataWithURL:urlStr Block:^(id result) {

        [_bgImageView sd_setImageWithURL:[NSURL URLWithString:result[@"backgroundLogo"]] placeholderImage:PLACEHOLDER_IMAGE];
        [_anchorImage sd_setImageWithURL:[NSURL URLWithString:result[@"mobileSmallLogo"]] placeholderImage:PLACEHOLDER_IMAGE];
        _personDescribe.text = result[@"personDescribe"];
    } ErrorBlock:^(NSError *error) {
        
        NSLog(@"anchorDetail 失败 = %@",error);
    }];
}
#pragma mark - 请求数据 刷新
-(void)getData
{
      NSString *urlStr = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/track/%ld/%ld/30?device=iPhone",self.uid,self.currentPage];
//      NSString *urlStr = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/track/%ld/1/30?device=iPhone",self.uid];
//    NSLog(@"zh == %@",urlStr);
    [AppTools getDataWithURL:urlStr Block:^(id result) {
//        NSLog(@"zh - result = %@",result);
        NSMutableArray *array = result[@"list"];
        for (NSDictionary *dic in array) {
            ZHAnchorDetailModel *detailModel = [ZHAnchorDetailModel modelWithDictionary:dic];
            CGSize simm = [SizeForLabel labelRectWithSize:CGSizeMake(SCREEN_WIDTH_ZH - 184, 1212) LabelText:detailModel.title Font:[UIFont systemFontOfSize:16.f]];
            detailModel.height = simm.height;
            [_detailArray addObject:detailModel];
        }
        _currentPage++;
        [_dTableView.mj_footer endRefreshing];
        [_dTableView reloadData];
    } ErrorBlock:^(NSError *error) {
        NSLog(@"ZHAnchorDetailVC 失败 == %@",error);
        
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - tableView协议方法
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    NSString *stringd = @"发布的声音";
    
    return stringd;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _detailArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifier = @"ZHAnchorDetailTableViewCell";
    ZHAnchorDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    ZHAnchorDetailModel *model = [_detailArray objectAtIndex:indexPath.row];
    cell.model = model;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZHAnchorDetailModel *detailModel = [_detailArray objectAtIndex:indexPath.row];
    
    return detailModel.height + 100;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    ZHPlayViewController *playerVC = [[ZHPlayViewController alloc]init];
    ZHNavigationBarController *naVC = [[ZHNavigationBarController alloc]initWithRootViewController:playerVC];
    ZHAnchorDetailModel *model = _detailArray[indexPath.row];
    playerVC.song_id = model.trackId;
    playerVC.allMusicArray = _detailArray;
    playerVC.indexRow = indexPath.row;
    [self presentViewController:naVC animated:YES completion:^{
        
        
    }];
}
#pragma mark - scrollView协议方法
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    /*
    // 让UITableView的section header view不悬停的方法,让表头跟着滚动
//    CGFloat sectionHeaderHeight = 0;
//    
//    if (scrollView.contentOffset.y <= sectionHeaderHeight && scrollView.contentOffset.y >= 0)
//    {
//        scrollView.contentInset = UIEdgeInsetsMake(- scrollView.contentOffset.y, 0, 0, 0);
//    }
//    else if (scrollView.contentOffset.y >= sectionHeaderHeight)
//    {
//        scrollView.contentInset = UIEdgeInsetsMake(- sectionHeaderHeight, 0, 0, 0);
//    }
//    */
    UIButton *button = (UIButton *)[self.navigationController.view viewWithTag:5555];
    if (scrollView.contentOffset.y < 0) {
        
        button.frame = CGRectMake(20, 0 , 20, 30);
        
        self.bgImageView.frame = CGRectMake(0, 0, SCREEN_WIDTH_ZH, SCREEN_HEIGHT_ZH*0.28-scrollView.contentOffset.y);
              _blurView.frame = CGRectMake(0, 0, SCREEN_WIDTH_ZH, SCREEN_HEIGHT_ZH*0.28-scrollView.contentOffset.y);
        _overImage.frame = CGRectMake(0, 0, SCREEN_WIDTH_ZH, SCREEN_HEIGHT_ZH*0.28-scrollView.contentOffset.y);
        
        _blurView.blurRadius = 1;
    }
    if (scrollView.contentOffset.y == 0) {
        
        button.frame = CGRectMake(20, 0 , 20, 30);
        
    }
    if (scrollView.contentOffset.y > 0) {
        
        // 隐藏button
        button.frame = CGRectMake(0, 0, 0, 0);
        
        self.bgImageView.frame = CGRectMake(0, -scrollView.contentOffset.y, SCREEN_WIDTH_ZH, SCREEN_HEIGHT_ZH*0.28);
        
    }
    
    if (scrollView.contentOffset.y == 0) {
        _blurView.blurRadius = 100;
    }
    
    [self.view bringSubviewToFront:self.dTableView];
    [self.view bringSubviewToFront:[ZHPlayButtonView shareButton]];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
